package db;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
/**
 * Hibernate会话工厂类
 * @author 请叫我小纯洁丶
 *
 */
public class MyHibernateSessionFactory {

	//会话工厂属性
	private static SessionFactory sessionFactory; 
	
	//私有化  保证单例模式
	private MyHibernateSessionFactory(){
		
	}
	
	//公有静态,获得会话工厂对象
	public static SessionFactory getSessionFactory(){
		
		if(sessionFactory == null){
			//生成配置对象
			Configuration configuration = new Configuration().configure();
			//创建服务注册对象
			ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
			//设置会话
			sessionFactory = configuration.buildSessionFactory(serviceRegistry);
			return sessionFactory;
			
		}else{
			
			return sessionFactory;
		}
		
		
	}
}
